<?php
class SupplierDishParser{

	public static function Instance(){
		return new SupplierDishParser();
	}

	public static function parseDishResultToXml($result_dish_category){
		$result_array = array();
		foreach ($result_dish_category as $dishCategory){
			$itemArray['DishCategoryId'] = $dishCategory->id;
			$itemArray['DishCategoryName'] = $dishCategory->name;

			$dishArray = array();
			$dish = ORM::factory("dish");
			$result_dish = $dish->where("dish_cate_id","=",$dishCategory->id)->and_where("status","=","A")->find_all();

			foreach ($result_dish as $item){
				$subItemArray['DishId'] = $item->id;
				$subItemArray['DishName'] = $item->name;
				if (!empty($item->picture)){
					if (file_exists(realpath(DISH_IMG_PATH.$item->picture))){
						$subItemArray['ImgUrl'] = URL::domain().DISH_IMG_PATH.$item->picture;
					}else{
						$subItemArray['ImgUrl'] = "";
					}
				}else{
					$subItemArray['ImgUrl'] = "";
				}

				$subItemArray['Desc'] = $item->desc;
				$subItemArray['Price'] = $item->price;
				$subItemArray['SecondPrice'] = $item->price2;
				$subItemArray['ThirdPrice'] = $item->price3;
				$subItemArray['DishCategoryId'] = $item->dish_cate_id;

				$dishItemArray = array();
				$result_dish_item = ORM::factory("dishItem")->where("status","=","A")->and_where("dishid","=",$item->id)->find_all();
				foreach ($result_dish_item as $ditem){
					$finalItem['DishItemId'] = $ditem->id;
					$finalItem['DishItemPrice'] = $ditem->price;
					$finalItem['DishItemSize'] = $ditem->displaySize();
					$finalItem['DishItemTemp'] = $ditem->displayTemp();
					$entity = new Entity();
					$entity->entityName = "DishItem";
					$entity->entityValue = $finalItem;
					array_push($dishItemArray, $entity);
				}
				$dishItemEntity = new Entity();
				$dishItemEntity->entityName = "DishItems";
				$dishItemEntity->entityValue = $dishItemArray;
				$subItemArray["DishItems"] = $dishItemEntity;
				
				$entity = new Entity();
				$entity->entityName = "Dish";
				$entity->entityValue = $subItemArray;
				array_push($dishArray, $entity);
			}

			$dishEntity = new Entity();
			$dishEntity->entityName = "Dishes";
			$dishEntity->entityValue = $dishArray;

			$itemArray['Dishes'] = $dishEntity;
			$entity = new Entity();
			$entity->entityName = "DishCategory";
			$entity->entityValue = $itemArray;
			array_push($result_array, $entity);
		}
		$entity = new Entity();
		$entity->entityName = "DishCategories";
		$entity->entityValue = $result_array;
		$xml = XmlUtil::Factory()->entityToXml($entity,0,0);
		return $xml;
	}
}